import scrapy
from stock_data_xuweijie.items import StockDataXuweijieItem


class TonghuashunSpider(scrapy.Spider):
    name = 'tonghuashun'
    allowed_domains = ['10jqka.com.cn']

    url = 'http://data.10jqka.com.cn/market/zdfph/'
    start_urls = [url]

    def parse(self, response):
        # with open("zdfph.html", "w", encoding="utf-8") as file:
        #     file.write(response.text)
        # //*[@id="J-ajax-main"]/table/tbody/tr[1]
        # //*[@id="J-ajax-main"]/table/tbody/tr[1]/td[3]/a

        stock_data_list = []

        for each in response.xpath("//*[@id=\"J-ajax-main\"]/table/tbody/tr"):
            # print("================================")

            name = each.xpath("td[3]/a/text()").extract()[0]
            # print(name)

            stockCode = each.xpath("td[2]/a/text()").extract()[0]
            # print(stockCode)

            price = each.xpath("td[4]/text()").extract()[0]
            # print(price)

            zdf = each.xpath("td[5]/text()").extract()[0]
            # print(zdf)

            hs = each.xpath("td[8]/text()").extract()[0]
            # print(hs)

            zj = each.xpath("td[11]/text()").extract()[0]
            # print(zj)

            rank = each.xpath("td[1]/text()").extract()[0]
            # print(rank)

            item = StockDataXuweijieItem()
            item['name'] = name
            item['stockCode'] = stockCode
            item['price'] = price
            item['zdf'] = zdf
            item['hs'] = hs
            item['zj'] = zj
            item['rank'] = rank

            stock_data_list.append(item)

        return stock_data_list